package models;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Id;

import play.data.validation.Required;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;


public class Focus extends AbstractBaseModel {
	static {
        ObjectifyService.register(Focus.class);
    }
	
	@Id public Long id;
	
	private Key<User> owner;
	@Required public String title;
	@Required public String description;
	private List<Key<User>> participants; // TODO

	public static Query<Focus> all() {
		return getService().query(Focus.class);
	}
	
	public static Focus getById(Long id) {
		return getService().get(new Key(Focus.class, id));
	}
	
	public List<Process> getAllProcess() {
		return Process.all().filter("focus", this).list();
	}
	 
	public void setOwner(User user) {
		this.owner = new Key<User>(User.class, user.getId());
	}
	
	public List<User> getParticipants() {
		ArrayList<User> users = new ArrayList<User>();
		if(this.participants != null) {
			for(Key<User> key : this.participants) users.add(User.getByKey(key));
		}
		return users;
	}
	
	public User getOwner() {
		return User.getByKey(this.owner);
	}
	
	public void addProcess(Process process) {
		process.setFocus(this);
		process.save();
	}
	
	public void addParticipant(User participant) {
		if(this.participants == null) {
			this.participants = new ArrayList<Key<User>>();
		}
		
		Key<User> userKey = new Key<User>(User.class, participant.getId());
		if(!this.participants.contains(userKey)) this.participants.add(userKey);
	}
	
	public void removeParticipant(User participant) {
		if(this.participants != null) {
			this.participants.remove(participant);
		}
	}
}
